/*            Copyright(c) 2006-2008 Dave Sexton           *
 * http://www.codeplex.com/DocProject                      *
 * http://www.codeplex.com/DocProject/Project/License.aspx *
 ***********************************************************/
using System;
using System.Collections.Generic;
using System.Text;
using VSLangProj80;
using EnvDTE;
using DaveSexton.DocProject.Configuration;
using DaveSexton.DocProject.Wizards.Foundation;
using System.Windows.Forms;

namespace DaveSexton.DocProject.Wizards
{
	[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Naming", "CA1710:IdentifiersShouldHaveCorrectSuffix")]
	public sealed class ChooseBuildEngineStep : WizardStep
	{
		#region Public Properties
		public override string Name
		{
			get
			{
				return WizardText.ChooseBuildEngineStepName;
			}
		}

		public override bool CanPreloadUserInterface
		{
			get
			{
				return true;
			}
		}

		public string SelectedBuildEngineProviderName
		{
			get
			{
				return control.SelectedBuildEngineProviderName;
			}
		}
		#endregion

		#region Private / Protected
		private ChooseBuildEngineControl control;
		private readonly IDocProject project;
		#endregion

		#region Constructors
		/// <summary>
		/// Constructs a new instance of the <see cref="ChooseBuildEngineStep" /> class.
		/// </summary>
		public ChooseBuildEngineStep(IDocProject project)
		{
			if (project == null)
				throw new ArgumentNullException("project");

			this.project = project;
		}
		#endregion

		#region Methods
		protected override object GetUserInterface(WizardController controller, bool preloading)
		{
			if (control == null)
			{
				control = new ChooseBuildEngineControl();
				control.SelectedBuildEngineProviderNameChanged += control_SelectedBuildEngineProviderNameChanged;
			}

			return control;
		}

		protected override bool CanNavigateAway(WizardController controller, WizardStep toStep, bool forward)
		{
			return control != null && control.BuildEngineProviderCount > 0;
		}

		protected override bool CanCancelWizard()
		{
			return control.BuildEngineProviderCount == 0;
		}

		protected override void WizardFinished(WizardController controller)
		{
			Save();
		}

		protected override void WizardCanceled(WizardController controller)
		{
			// Even if the wizard is canceled VS will have created the new DocProject or DocSite, so this step is required
			// to set the selected build engine so that the project is usable in VS.  If the build engine's step isn't completed
			// then the user can perform any necessary operations manually to get the project working for the selected 
			// build engine.
			Save();
		}

		private void Save()
		{
			DocProjectOptions.SetBuildEngineProvider(project, SelectedBuildEngineProviderName);

			DocProjectEnvironment.DefaultBuildEngineProvider = project.Provider;
		}
		#endregion

		#region Event Handlers
		private void control_SelectedBuildEngineProviderNameChanged(object sender, EventArgs e)
		{
			WizardStep step = NextStep;

			if (step != null)
			{
				do
				{
					step.Dispose();
					step = step.NextStep;
				}
				while (step != null);

				UnlinkFromNextStep();
			}

			WizardStep providerStep = project.Provider.CreateNewProjectWizardStep(project);

			if (providerStep != null)
				providerStep.InjectAfter(this);

			providerStep.PreloadUserInterfacesFromHere(true);
		}
		#endregion
	}
}
